// 
// Decompiled by Procyon v0.5.36
// 

package com.orange.boot.dev2.service.impl;

import com.orange.boot.dev2.entity.request.ForecastRequest;
import com.orange.boot.dev2.entity.vo.ForecastVO;
import org.springframework.stereotype.Service;
import com.orange.boot.dev2.service.CatalogueService;
import com.orange.boot.dev2.entity.Catalogue;
import com.orange.boot.dev2.mapper.CatalogueMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.HashMap;
import java.util.Map;

@Service
public class CatalogueServiceImpl extends ServiceImpl<CatalogueMapper, Catalogue> implements CatalogueService {
    @Override
    public ForecastVO performForecast(ForecastRequest request) {
        ForecastVO forecastVO = new ForecastVO();

        int[] parameters = predictEnvironment(request.getModel(), request.getParameter());
        forecastVO.setRes(parameters);

        String support = generateSupport(request.getParameter(), parameters);
        forecastVO.setSupport(support);

        return forecastVO;
    }

    private int[] predictEnvironment(String model, String parameter) {
        switch (parameter) {
            case "二氧化碳":
            case "光照强度":
            case "pm2.5":
            case "pm10":
            case "空气温度":
            case "空气湿度":
                return getModelSpecificParameters(model, parameter);
            default:
                throw new IllegalArgumentException("未知的环境参数: " + parameter);
        }
    }

    private int[] getModelSpecificParameters(String model, String parameter) {
        Map<String, Map<String, int[]>> modelParametersMap = new HashMap<>();

        // 填充线性回归模型数据
        Map<String, int[]> linearRegressionParameters = new HashMap<>();
        linearRegressionParameters.put("二氧化碳", new int[]{310, 400, 456, 512, 600, 678, 550, 455, 404, 391, 373, 351});
        linearRegressionParameters.put("光照强度", new int[]{12013, 13111, 14541, 15134, 17234, 18543, 17563, 16103, 14545, 12031});
        linearRegressionParameters.put("pm2.5", new int[]{68, 63, 58, 54, 50, 49, 52, 53, 68, 69, 69, 70, 65});
        linearRegressionParameters.put("pm10", new int[]{47, 45, 45, 40, 39, 41, 42, 43, 46, 46, 47, 48});
        linearRegressionParameters.put("空气温度", new int[]{23, 26, 26, 28, 29, 30, 29, 28, 27, 27, 24, 23});
        linearRegressionParameters.put("空气湿度", new int[]{63, 57, 52, 52, 45, 46, 50, 55, 60, 67, 65, 71});
        modelParametersMap.put("线性回归", linearRegressionParameters);

        // 填充APIMA模型数据
        Map<String, int[]> decisionTreeParameters = new HashMap<>();
        decisionTreeParameters.put("二氧化碳", new int[]{320, 410, 460, 520, 610, 680, 560, 460, 410, 400, 380, 360});
        decisionTreeParameters.put("光照强度", new int[]{12500, 13500, 15000, 16000, 17500, 19000, 18000, 16500, 15000, 12500});
        decisionTreeParameters.put("pm2.5", new int[]{70, 65, 60, 55, 52, 50, 55, 58, 70, 72, 70, 75});
        decisionTreeParameters.put("pm10", new int[]{50, 48, 46, 42, 40, 44, 45, 48, 50, 50, 50, 52});
        decisionTreeParameters.put("空气温度", new int[]{22, 25, 27, 29, 30, 32, 30, 29, 28, 26, 24, 22});
        decisionTreeParameters.put("空气湿度", new int[]{60, 55, 50, 53, 47, 48, 52, 57, 62, 68, 66, 70});
        modelParametersMap.put("APIMA模型", decisionTreeParameters);

        // 填充支持向量机(SVM)数据
        Map<String, int[]> neuralNetworkParameters = new HashMap<>();
        neuralNetworkParameters.put("二氧化碳", new int[]{330, 420, 470, 530, 620, 690, 570, 470, 420, 410, 390, 370});
        neuralNetworkParameters.put("光照强度", new int[]{13000, 14000, 15500, 16500, 18000, 19500, 18500, 17000, 15500, 13000});
        neuralNetworkParameters.put("pm2.5", new int[]{72, 67, 62, 57, 55, 53, 57, 60, 72, 75, 72, 78});
        neuralNetworkParameters.put("pm10", new int[]{52, 50, 48, 44, 42, 46, 48, 50, 52, 52, 52, 54});
        neuralNetworkParameters.put("空气温度", new int[]{21, 24, 26, 28, 31, 33, 31, 30, 29, 27, 25, 21});
        neuralNetworkParameters.put("空气湿度", new int[]{58, 53, 48, 50, 45, 46, 51, 56, 61, 66, 64, 68});
        modelParametersMap.put("支持向量机(SVM)", neuralNetworkParameters);

        // 返回对应模型和参数的预测数组
        if (modelParametersMap.containsKey(model)) {
            Map<String, int[]> parametersMap = modelParametersMap.get(model);
            return parametersMap.getOrDefault(parameter, new int[]{});
        } else {
            // 如果模型不存在，返回空数组
            return new int[]{};
        }
    }

    private String generateSupport(String parameter, int[] parameters) {
        // 获取预测数组中的最大值和最小值
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;
        for (int param : parameters) {
            if (param > max) max = param;
            if (param < min) min = param;
        }

        // 生成建议信息
        switch (parameter) {
            case "二氧化碳":
                if (max > 700) {
                    return "二氧化碳预测所得结果较高，建议适当通风以减少二氧化碳浓度，并保持充足的光照。";
                } else {
                    return "二氧化碳预测所得结果为种植适宜条件，建议维持现状并保持充足的光照。";
                }
            case "光照强度":
                if (min < 10000) {
                    return "光照强度预测所得结果较低，建议增加光照以确保植物生长。";
                } else {
                    return "光照强度预测所得结果为种植适宜条件，建议维持现状。";
                }
            case "pm2.5":
                if (max > 75) {
                    return "pm2.5预测所得结果较高，建议加强通风以改善空气质量。";
                } else {
                    return "pm2.5预测所得结果为种植适宜条件，建议维持现状并保持通风。";
                }
            case "pm10":
                if (max > 50) {
                    return "pm10预测所得结果较高，建议加强通风以改善空气质量。";
                } else {
                    return "pm10预测所得结果为种植适宜条件，建议维持现状并保持通风。";
                }
            case "空气温度":
                if (min < 15 || max > 35) {
                    return "温度预测所得结果显示部分时间不适宜，建议采取加热或降温措施以维持适宜温度。";
                } else {
                    return "温度预测所得结果为种植适宜条件，建议维持现状。";
                }
            case "空气湿度":
                if (min < 40) {
                    return "空气湿度预测所得结果偏低，建议喷水提高空气湿度。";
                } else if (max > 80) {
                    return "空气湿度预测所得结果偏高，建议通风以降低空气湿度。";
                } else {
                    return "空气湿度预测所得结果为种植适宜条件，建议维持现状。";
                }
            default:
                return "没有具体的建议信息。";
        }
    }

}
